package cs.xwi7xwa.templates;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.code.microlog4android.Logger;
import com.google.code.microlog4android.LoggerFactory;

import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import cs.xwi7xwa.R;
import cs.xwi7xwa.activities.ExhibitListView;
import cs.xwi7xwa.activities.HomeView;
import cs.xwi7xwa.activities.MyFactsView;

public abstract class BumpTemplate extends Activity
{
    protected static final DateFormat DATE_FORMAT = 
        new SimpleDateFormat("MMM dd yyyy, HH:mm:ss");
    protected static final Logger LOGGER = LoggerFactory.getLogger();
    
    // TODO: See if ListActivity override any Activity's method
    // that we need. If not, only one of Bump*Template should stay
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Display the menu and its buttons from options_menu.xml
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	Intent intent;
    	
        switch (item.getItemId())
        {
        case R.id.homeMenuItem:
        	// Open HomeView
        	intent = new Intent(this, HomeView.class);
        	startActivityForResult(intent, 0);
        	return true;
        case R.id.exhibitsMenuItem:
            // Open ExhibitsListView
            intent = new Intent(this, ExhibitListView.class);
            startActivityForResult(intent, 0);
            return true;
        case R.id.mapMenuItem:
            // Display an error message
            View layout = getLayoutInflater().inflate(R.layout.toast,
                    (ViewGroup) findViewById(R.id.toastText));
            Xwi7xwaToast notImplemented = new Xwi7xwaToast(layout,
                    getApplicationContext(),
                    "Sorry, this feature has not been developed.");
            notImplemented.show();
            return true;
        case R.id.myFactsMenuItem:
            // Open MyFactsView
            intent = new Intent(this, MyFactsView.class);
            startActivityForResult(intent, 0);
            return true;
        default:
            return true;
        }
    }
    
    /**
     * Log the execution of the caller method and the current time
     * Class.method: HH:MM:SS [Parameter1] [Parameter2] ... 
     * 
     * @param logger Logger to log this event
     * @param parameters Additional information for this event
     */
    protected void log(String[] parameters)
    {
        // Exception is created since caller is get thru a stacktrace.
        Exception exception = new Exception();
        
        StringBuilder log = new StringBuilder(
                exception.getStackTrace()[1].getClassName() + "."
                + exception.getStackTrace()[1].getMethodName() + ": "
                + DATE_FORMAT.format(new Date()));
        
        if (parameters != null)
        {
            for (String parameter : parameters)
            {
                log.append(" [").append(parameter).append("]");
            }
        }
        
        LOGGER.info(log.toString());
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            log(new String[]{"Back button pressed"});
        }
        return super.onKeyDown(keyCode, event);
    }
}